package com.mask.mk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mask.mk.pojo.Addressinfo;
import com.mask.mk.mapper.AddressinfoMapper;
import com.mask.mk.pojo.Goods;
import com.mask.mk.service.AddressinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.mk.util.JsonResult;
import com.mask.mk.vo.address.AddressResponse;
import com.mask.mk.vo.address.UpdateAddressRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 地址信息表 服务实现类
 * </p>
 *
 * @author joey
 * @since 2023-04-25
 */
@Service
public class AddressinfoServiceImpl implements AddressinfoService {

    @Autowired
    private AddressinfoMapper addressinfoMapper;


    //将地址状态置为其他
    public void updateAddress(int userId){
        List<Addressinfo> addressinfos = addressinfoMapper.selectList(Wrappers.<Addressinfo>lambdaQuery()
                .and(wrapper -> wrapper.eq(Addressinfo::getUserid, userId)));
        for (Addressinfo addressinfo : addressinfos) {
            addressinfo.setDefaultStatus(0);
            addressinfoMapper.updateById(addressinfo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addUpdateAddressById(UpdateAddressRequest updateAdReq) {

        JsonResult result = null;
        //判断增加还是更新
        Addressinfo addressinfo = addressinfoMapper.selectById(updateAdReq.getAddressId());

        if(addressinfo==null){ //新增
            if(updateAdReq.getAdStatus()==1){
                updateAddress(updateAdReq.getUserId());
            }
            Addressinfo newAddress = new Addressinfo();
            newAddress.setAdinfo(updateAdReq.getAdInfo())
                    .setDetailInfo(updateAdReq.getDetailInfo())
                    .setUserName(updateAdReq.getUserName())
                    .setUserPhone(updateAdReq.getUserPhone())
                    .setDefaultStatus(updateAdReq.getAdStatus())
                    .setUserid(updateAdReq.getUserId());
            int insert = addressinfoMapper.insert(newAddress);
            if(insert>0){
                result = JsonResult.success(newAddress);
            }else{
                result = JsonResult.fail("添加失败");
            }

        }else{//修改
            if(updateAdReq.getAdStatus()==1){
                updateAddress(updateAdReq.getUserId());
            }
            addressinfo.setDefaultStatus(updateAdReq.getAdStatus())
                    .setAdinfo(updateAdReq.getAdInfo())
                    .setDetailInfo(updateAdReq.getDetailInfo())
                    .setUserName(updateAdReq.getUserName())
                    .setUserPhone(updateAdReq.getUserPhone());
            int updateById = addressinfoMapper.updateById(addressinfo);
            if(updateById>0){
                result = JsonResult.success(addressinfo);
            }else{
                result = JsonResult.fail("修改失败");
            }
        }

        return result;
    }

        //默认1 其他0

    @Override
    public JsonResult queryOneDefault(UpdateAddressRequest updateAddressRequest) {
        JsonResult result = null;
        Addressinfo addressinfo = addressinfoMapper.selectOne(Wrappers.<Addressinfo>lambdaQuery()
                .and(wrapper -> wrapper.eq(Addressinfo::getUserid, updateAddressRequest.getUserId())
                        .eq(Addressinfo::getDefaultStatus, updateAddressRequest.getAdStatus())));
        boolean adf = false;
        if(updateAddressRequest.getAdStatus()==1){
            adf = true;
        }else{
            adf = false;
        }
        AddressResponse addressResponse = new AddressResponse();
        addressResponse
                .setAddressId(addressinfo.getAddressid())
                .setAddefault(adf)
                .setAddress("")
                .setAddressInfo(addressinfo.getAdinfo())
                .setAddressDetail(addressinfo.getDetailInfo())
                .setName(addressinfo.getUserName())
                .setPhone(addressinfo.getUserPhone())
                .setLastname("")
                .setFormshow(false);
        if(addressResponse!=null){
            result = JsonResult.success(addressResponse);
        }else{
            result = JsonResult.fail("当前没有默认地址");
        }
        return result;
    }

    @Override
    public JsonResult queryOneById(UpdateAddressRequest updateAddressRequest) {

        return null;
    }

    @Override
    public JsonResult queryAddressList(UpdateAddressRequest updateAddressRequest) {
//        Addressinfo addressinfo = new Addressinfo();

        QueryWrapper<Addressinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",updateAddressRequest.getUserId());
        wrapper.orderByDesc("update_time");
        List<Addressinfo> addressinfoList = addressinfoMapper.selectList(wrapper);

        if(addressinfoList!=null){
            return JsonResult.success(addressinfoList);
        }else{
            return JsonResult.fail("获取列表异常");
        }


    }

    @Override
    public JsonResult delAddress(UpdateAddressRequest updateAddressRequest) {
        int deleteById = addressinfoMapper.deleteById(updateAddressRequest.getAddressId());
        if(deleteById>0){
            return JsonResult.success("删除成功");
        }else{
            return JsonResult.fail("删除失败");
        }
    }
}
